import {
  getSuggestList,
  dispatchSuggest,
  deleteSuggest
} from '../../../services/api/message'
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    title: "",
    showFilter: false,
    status: '',
    processStatus: {
      '2': {
        type: 'success',
        text: '已关闭'
      },
      '0': {
        type: 'danger',
        text: '未分配'
      },
      1: {
        type: 'warning',
        text: '已分配待处理'
      },
    },
    goodsListLoadStatus: 0,
    pageLoading: false,
    processEmpUserId: '',
    loadMoreStatus: 0,
    loading: true,
    userList: [],
    popvisible: false,
    menberList: [],
    kid: '',
    tabList: [{
        name: '未分配',
        value: 0,
      },
      {
        name: '已分配待处理',
        value: 1
      },
      {
        name: '已完成',
        value: 2
      },
    ],
    isAgentStatus: true,
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  onLoad: function () {
    var that = this;
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查看options
    this.getList();
  },
  clear() {
    this.setData({
      title: ''
    });
    this.loadHomePage(true)
  },
  confirm() {
    // this.getList();
    this.setData({
      showFilter: false
    })
    this.loadGoodsList(true)
  },
  reset() {
    this.setData({
      visible: false,
      title: '',
      status: "",
    })
    this.loadGoodsList(true)
  },
  submit() {
    this.loadGoodsList(true);
  },
  onChange2(e) {
    this.setData({
      'status': e.detail.value,
    });
  },
  onVisibleChange2(e) {
    this.setData({
      showFilter: e.detail.visible,
    });
  },
  searcnFn() {
    this.setData({
      showFilter: true
    })
  },
  toDetail(e) {
    wx.navigateTo({
      url: `/subpages/pages/suggestAndAdvice/suggestAndAdvice?isEdit=true&kid=${e.currentTarget.dataset.group.kid}&hideButton=true&processStatus=${e.currentTarget.dataset.group.processStatus}`,
    })
  },
  async getList() {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    let params = {
      "pageNo": 1,
      "pageSize": 10,
      "param": {
        // "empUserIds": [
        //   userId
        // ],
        title: this.data.title,
        processStatus: this.data.status,
      }
    };

    let {
      data
    } = await getSuggestList(params);
    this.setData({
      userList: data.entities
    })
  },
  add() {
    wx.navigateTo({
      url: '/subpages/pages/suggestAndAdvice/suggestAndAdvice?hideButton=false',
    })
  },
  async onDelete(e) {
    // 操作投诉建议 1:删除（仅限代理商端操作，且只有未处理的投诉建议才可以） opType 1
    let group = e.currentTarget.dataset.group;
    let res = await deleteSuggest({
      kid: group.kid,
      opType: 1
    });
    let index = this.data.userList.findIndex((el) => el.kid == group.kid);
    if (res.code == '200') {
      Toast({
        selector: '#t-toast',
        message: '删除成功',
        icon: '',
        duration: 1000,
      });
      let data =
        this.data.userList.splice(index, 1);
      this.setData({
        userList: this.data.userList
      })
    }
  },
  onShow() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReTry() {
    this.loadGoodsList();
  },
  async loadGoodsList(fresh = false) {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1
    });
    const pageSize = this.goodListPagination.pageSize;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const {
        data
      } = await getSuggestList({
        "pageNo": pageIndex,
        "pageSize": pageSize,
        "param": {
          title: this.data.title,
          processStatus: this.data.status,
        }
      });
      this.setData({
        userList: fresh ? data.entities : this.data.userList.concat(data.entities),
        goodsListLoadStatus: data.entities.length > 1 ? 0 : 2,
      });

      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

})